package PenTest.alibaba.ali;

public class SequencePrint{
    private int count=0;
    private final Object lock=new Object();
    public static void main(String[] args){
        SequencePrint printer=new SequencePrint();
        //多个线程
        Thread threadX=new Thread(()->printer.print(1,'X'),"X");
        Thread threadY=new Thread(()->printer.print(2,'Y'),"Y");
        Thread threadZ=new Thread(()->printer.print(3,'Z'),"Z");
        threadX.start();
        threadY.start();
        threadZ.start();
    }
    //使用对象锁控制打印
    private void print(int number,Character character){
        for(int i=0;i<10;i++){
            synchronized(lock){
                while(count%3!=number-1){
                    try {
                      lock.wait();
                    } catch (InterruptedException e) {
                      throw new RuntimeException(e);
                    }
                }
                if(count<30){
                      System.out.println(""+character);
                      count++;
                      lock.notifyAll();
                }
            }
        }
    }
}